﻿using System;
using DHJ.Cost.EntityFrameworkCore;
using DHJ.Cost.ManHours;
using DHJ.Cost.NumToString;
using DHJ.Cost.Tasks;
using DHJ.Cost.Timing;
using DHJ.Cost.Workshops;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using Abp.Timing;
using DHJ.Cost.Models;

namespace DHJ.Cost.Migrations.Seed.Host
{
    public class DefaultModelsCreator
    {
        private readonly CostDbContext _context;

        public DefaultModelsCreator(CostDbContext context)
        {
            _context = context;
        }

        public void Create()
        {
            CreateModels();
        }

        private void CreateModels()
        {

            var model = new Models.Model("CZ-3A", "长征三号甲", "机动式中程弹道导弹");
            CreateModelIfNotExists(model);

            model = new Models.Model("CZ-3B", "长征三号乙", "国际发射服务市场上的主推火箭");
            CreateModelIfNotExists(model);


            model = new Models.Model("CZ-5", "长征五号", "中国新一代大型运载火箭。");
            CreateModelIfNotExists(model);

            model = new Models.Model("CZ-5B", "长征三号乙", "中国新一代大型运载火箭");
            CreateModelIfNotExists(model);

            model = new Models.Model("CZ-7", "长征七号", "中国新一代大型运载火箭");
            CreateModelIfNotExists(model);
            model = new Models.Model("CZ-7A", "长征七号甲", "新一代中型高轨运载火箭");
            CreateModelIfNotExists(model);

            model = new Models.Model("CZ-8", "长征八号", "国际发射服务市场上的主推火箭");
            CreateModelIfNotExists(model);

            model = new Models.Model("CZ-3B", "长征三号乙", "面向商业市场的新型火箭。");
            CreateModelIfNotExists(model);

            //var pici1 = new Pici()
            //{
            //    Name = "批次1",
            //    Model = "CZ-XX1",
            //    TaskPrefixes = "T",
            //    Quantity = 1
            //};
            //var pici2 = new Pici()
            //{
            //    Name = "批次2",
            //    Model = "CZ-XX1",
            //    TaskPrefixes = "T1,T2",
            //    Quantity = 2
            //};

            //CreatePiciIfNotExists(pici1);
            //CreatePiciIfNotExists(pici2);
        }

        private void CreateModelIfNotExists(Models.Model model)
        {
            var queryTask = _context.Models.IgnoreQueryFilters()
                .FirstOrDefault(ef => ef.Code == model.Code);

            if (queryTask == null)
            {
                _context.Models.Add(model);

                _context.SaveChanges();
            }
        }

        private void CreateFaciIfNotExists(Launch launch)
        {
            var queryBatch = _context.Launches.IgnoreQueryFilters()
                .FirstOrDefault(ef => ef.Code == launch.Code);

            if (queryBatch == null)
            {
                _context.Launches.Add(launch);

                _context.SaveChanges();
            }
        }

        private void CreatePiciIfNotExists(Pici pici)
        {
            var queryBatch = _context.Pici.IgnoreQueryFilters()
                .FirstOrDefault(ef => ef.Name == pici.Name);

            if (queryBatch == null)
            {
                _context.Pici.Add(pici);

                _context.SaveChanges();
            }
        }
    }
}